package com.ingweb.inventarius.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.ingweb.inventarius.dao.DAOInventario;
import com.ingweb.inventarius.model.Inventario;

/***
 * 
 * 
 * Clase que implementa los metodos de la interfaz DAOInventario
 * 
 * @see DAOInventario
 *
 */
public class DAOInventarioImpl implements DAOInventario {

	private static Logger logger = Logger.getLogger(DAOInventarioImpl.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingweb.hibernate.dao.DAOInventario#obtenerInventarios()
	 */
	@Override
	public List<Inventario> obtenerInventarios() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		List<Inventario> encontrados = new ArrayList<Inventario>();

		try {
			encontrados = session.createQuery("from Inventario").list();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada " + ex.getLocalizedMessage());
		} finally {
			session.close();
		}

		return encontrados;
	}

	@Override
	public Inventario obtenerInventario(int codigo) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Inventario encontrado = null;

		try {
			encontrado = (Inventario) session.get(Inventario.class, codigo);
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}

		return encontrado;
	}

	@Override
	public void guardarInventario(Inventario inventario) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();

		try {
			session.save(inventario);
			session.getTransaction().commit();

		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
	}

	@Override
	public void eliminarInventario(Inventario inventario) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();

		try {
			session.delete(inventario);
			session.getTransaction().commit();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
	}

	@Override
	public void editarInventario(Inventario inventario) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		session.beginTransaction();

		try {
			session.merge(inventario);
			session.getTransaction().commit();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}
		
	}

	@Override
	public List<Inventario> buscarInventario(String cadena) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(Inventario.class);
		List<Inventario> encontrados = new ArrayList<Inventario>();

		try {
			criteria.add(Restrictions.like("cantProd", "%" + cadena + "%"));
			encontrados = criteria.list();
		} catch (HibernateException ex) {
			logger.debug("Exception encontrada" + ex.getLocalizedMessage());
		} finally {
			session.close();
		}

		return encontrados;
	}

}
